<?php
/**
 * Created by JetBrains PhpStorm.
 * User: heimuya
 * Date: 14-1-26
 * Time: 下午1:33
 * 个人信息设置控制器类
 */
class SetAction extends AdminAction{
	
	public function _initialize(){
		parent::_initialize();
		$this->assign('page', __CLASS__);
	}
	
    /**
     * 后台首页
     */
    public function index(){
        $type = $this->_get('type');
        switch($type){
            case 1:
                $errorMsg = L('currentpwdError');
                break;
            case 2:
                $errorMsg = L('newpwdError');
                break;
            case 3:
                $errorMsg = L('unequalError');
                break;
            case 4:
                $errorMsg = L('saveSuccess');
                break;
            default:
                $errorMsg = '';
                break;
        }
        $this->assign('errorMsg', $errorMsg);
        $this->display();
    }
	
	
    /**
     * 后台首页
     */
    public function changepwd(){
        $postData = $this->_post();
		if(!isset($postData['newpassword']) || $postData['newpassword'] == ''){//用户名或者密码为空
            $this->redirect('Set/index', array('type' => 2));
			exit(0);
        }
		if(!isset($postData['confirmpassword']) || $postData['confirmpassword'] != $postData['newpassword']){//用户名或者密码为空
            $this->redirect('Set/index', array('type' => 3));
			exit(0);
        }
		$user = D('User');
        if(!($userInfo = $user->getUserInfo(session('unick'))) || $userInfo['password'] != md5($postData['currentpassword'])){//用户名或者密码错误
            $this->redirect('Set/index', array('type' => 1));
			exit(0);
        }
		$data['password'] = md5($postData['confirmpassword']);
		$where['id'] = session('uid');
		$user->where($where)->save($data);
		$this->redirect('Set/index', array('type' => 4));
    }
	
	
	/**
	 * 修改管理员界面theme
	 */
	 public function themeedit(){
	 	//异步提交的数据
	 	$color = $this->_post('color') !== '' ? $this->_post('color') : 'default';
		$data['csstype'] = $color;
		$where['id'] = session('uid');
		D('User')->where($where)->save($data);
		session('csstype', $color);
		echo json_encode(array('status' => 200));
		exit(0);
	 }
}